﻿Public Delegate Sub SimpleCallback()

Public Class TransitionBase

    Private mTimeSoFar As Single
    Private mTransitionTime As Single
    Private mFractionComplete As Single
    Private mOnComplete As SimpleCallback

    Public Sub New(ByVal transitionTime As Single, ByVal onComplete As SimpleCallback)
        mTransitionTime = transitionTime
        mOnComplete = onComplete
    End Sub

#Region " Properties "

    Public ReadOnly Property FractionComplete() As Single
        Get
            FractionComplete = mFractionComplete
        End Get
    End Property

    Public ReadOnly Property FractionIncomplete() As Single
        Get
            FractionIncomplete = 1 - FractionComplete
        End Get
    End Property

    ''' <summary>
    ''' Gets the current alpha of the screen transition, ranging
    ''' from 255 (fully active, no transition) to 0 (transitioned
    ''' fully off to nothing).
    ''' </summary>
    Public ReadOnly Property TransitionAlpha() As Byte
        Get
            TransitionAlpha = CByte(255 - mFractionComplete * 255)
        End Get
    End Property

    Public ReadOnly Property TransitionAlphaByHalves() As Byte
        Get
            If mFractionComplete > 0.5F Then
                TransitionAlphaByHalves = CByte((255 - mFractionComplete * 510) * -1.0F)
            Else
                TransitionAlphaByHalves = CByte(255 - mFractionComplete * 510)
            End If
        End Get
    End Property

    Public ReadOnly Property TransitionTime() As Single
        Get
            TransitionTime = mTransitionTime
        End Get
    End Property

#End Region ' Properties

    Public Overridable Sub Update(ByVal elapsedSeconds As Single)
        mTimeSoFar += elapsedSeconds
        If mTimeSoFar >= mTransitionTime Then
            mFractionComplete = 1.0F
            mOnComplete.Invoke()
        Else
            mFractionComplete = mTimeSoFar / mTransitionTime
        End If
    End Sub

End Class